/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.miscellaneous.magnifier;

import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.MouseEvent;

import twaver.Element;
import twaver.MovableFilter;
import twaver.TWaverUtil;
import twaver.network.TNetwork;
import twaver.network.inputhandler.InputAdapter;

public class MagnifierHandler extends InputAdapter {
	
    private MagnifierNetwork network = null;
    private Point lastPoint = null;
    private MovableFilter filter = null;
    
    public MagnifierHandler(final TNetwork network) {
        this.network = (MagnifierNetwork)network;
        this.filter = new MovableFilter(){
			public boolean isMovable(Element element) {
				return network.isVisible(element);
			}
        };        
    }

    public void init(){
    	network.enableMagnifier(true);
    }

    public void cleanup(){
    	network.enableMagnifier(false);
    }

    public void mouseMoved(MouseEvent e) {
    	this.lastPoint = e.getPoint();    
    	if(network.isLegalPoint(lastPoint)){
    		network.getCanvas().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    	}else{
    		network.getCanvas().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    	} 
    }    
    
    public void mouseDragged(MouseEvent e) {   	
    	if(network.isLegalPoint(lastPoint)){
	        double xOffset = (e.getX() - this.lastPoint.getX()) / this.network.getZoom() / this.network.getMagnifierZoom();
	        double yOffset = (e.getY() - this.lastPoint.getY()) / this.network.getZoom() / this.network.getMagnifierZoom();
	        TWaverUtil.moveElements(network.getDataBox().iterator(), filter, xOffset, yOffset);
	        network.getCanvas().repaint(); 		
    	}
    	this.mouseMoved(e);
    }
}